package nuc.soft.xsf.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import nuc.soft.xsf.model.ProjectTest;
import nuc.soft.xsf.model.TitletypeProject;
import nuc.soft.xsf.service.IProjectTestService;
import nuc.soft.xsf.service.ITitleTypeProjectService;
import nuc.soft.xsf.service.ITitletypeService;
import nuc.soft.xsf.tools.Tools;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/ProjectController")
public class ProjectController {
	@Resource
	private IProjectTestService projectService;

	@Resource
	private ITitleTypeProjectService titleTypeProjectService;

	@RequestMapping(value = "/add.action", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addProjectTest(ProjectTest project) {
		project.setDeletestate(0);
		int id = projectService.insert(project);

		if ((project.getProjectParentId()) != -1) {
			String[] arr = project.getExtend2().split(",");
			for (int j = 0; j < arr.length; j++) {
				TitletypeProject titleTypeProject = new TitletypeProject();
				titleTypeProject.setDeletestate(0);
				titleTypeProject.setProjectId(id);
				if (arr.length > 0) {
					titleTypeProject.setTitleTypeid(Integer.parseInt(arr[j]));
				}
				titleTypeProjectService.insert(titleTypeProject);
			}
		}
		JSONObject json = new JSONObject();
		json.put("data", project);
		return json.toString();
	}

	@RequestMapping(value = "/findByCondition.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String finByCondition(ProjectTest project) {
		Map map = Tools.transBean2Map(project);
		List<ProjectTest> list = projectService.findByCondition(map);
		JSONObject json = new JSONObject();
		System.out.println(list);
		json.put("list", list);
		return json.getString("list");
	}

	@RequestMapping(value = "/update.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String update(ProjectTest project) {
		// 将之前的所有数据都清除关联
		if (project.getProjectParentId() != -1) {
			TitletypeProject titleProject = new TitletypeProject();
			titleProject.setProjectId(project.getProjectId());
			Map map = Tools.transBean2Map(titleProject);
			List<TitletypeProject> list = titleTypeProjectService.findByCondition(map);
			for (int i = 0; i < list.size(); i++) {
				TitletypeProject titleProject01 = list.get(i);
				titleTypeProjectService.deleteByProjectId(titleProject01
						.getProjectId());
			}

			projectService.update(project);

			String[] arr = project.getExtend2().split(",");
			if (arr != null && arr.length > 0) {
				for (int j = 0; j < arr.length; j++) {
					TitletypeProject titleTypeProject = new TitletypeProject();
					titleTypeProject.setDeletestate(0);
					titleTypeProject.setProjectId(project.getProjectId());
					titleTypeProject.setTitleTypeid(Integer.parseInt(arr[j]));
					titleTypeProjectService.insert(titleTypeProject);
				}
			}
		} else {
			projectService.update(project);
		}
		ProjectTest project1 = projectService.findById(project.getProjectId());
		JSONObject json = new JSONObject();
		json.put("data", project1);
		json.put("redata", project1.getProjectId());
		return json.toString();
	}

	@RequestMapping(value = "/delete.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String delete(String ids) {
		String[] str = ids.split(",");
		JSONObject json = new JSONObject();
		int success = 0;
		for (String strTmp : str) {
			ProjectTest project = projectService.findById(Integer
					.parseInt(strTmp));
			TitletypeProject titleProject = new TitletypeProject();
			titleProject.setProjectId(Integer.parseInt(strTmp));
			Map map = Tools.transBean2Map(titleProject);
			List<TitletypeProject> list = titleTypeProjectService
					.findByCondition(map);

			if (project != null) {
				project.setDeletestate(1);
				projectService.update(project);
				success++;
			}

			for (int i = 0; i < list.size(); i++) {
				TitletypeProject titleProject01 = list.get(i);
				titleProject01.setDeletestate(1);
				titleTypeProjectService.update(titleProject01);

			}
		}
		json.put("success", success);
		return json.toString();
	}
}
